<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Common\CommonController;
//哪个控制器需要权限控制就加上权限继承
class UserController extends CommonController {
    public function index(){
    	//分页
    	$link = M();
   		$sql_all ="select count(*) cn from user where deleted ='0'";
   		$res =current($link->query($sql_all))['cn'];
   		 $Page = new \Think\Page($res,4);
   		 $show =$Page->show();

    	
    	//将用户数据输出到模板
   		$sql_user = "select * from user where deleted ='0' order by regist_time asc limit {$Page->firstRow},{$Page->listRows}";
   		$data = $link->query($sql_user);
   		// var_dump(count($res));


   		$this->assign('show',$show);//将分页输出
   		$this->assign('data',$data);//将用户数据输出到模板
        $this->display();
    }


    //*****************给用户授权
    public function impower(){
      //输出角色名
      $link = M();
      $sql = "select id,title from tp_auth_group ";
      // var_dump($sql);
      $data = $link->query($sql);
      $this->assign('data',$data);//将角色输出
      // var_dump($data);

      if ($_GET) {
       $id =$_GET['id'];//获取要增加授权的用户的id
       // var_dump($id);
       $sql_user ="select username from user where id='{$id}'";
       $user =$link->query($sql_user);
       // var_dump($user);
        $this->assign('user',current($user));//将需要授权的用户的名字输出
      }
      

      if ($_POST) {
        $role=I('post.title');//选择的角色id
        // var_dump($role[0]);
        $sql_role ="insert into tp_auth_group_access (uid,group_id) values('{$id}','{$role[0]}') ";
        // var_dump($sql_role);
        $res=$link->execute($sql_role);
        $this->success('授权成功',U('admin/user/successd'));
      }else{
        $this->display();
      }

      
    }

  

    //*****************添加用户
   public function user_add(){
   	if ($_POST) {
   		// var_dump($_POST);

   		//添加用户
   		$username = I('post.user_name');
   		$password = md5(I('post.password'));
   		$sex = I('post.sex');
   		$phone = I('post.user_tel');
   		$email = I('post.email');
   		$head_img = "/Uploads/face/".I('post.uploadfile_2');
   		$address = I('post.demo1');
   		$introduce = I('post.introduce');
   		$regist_time = date('Y-m-d H:m:s');
   		$sql = "insert into user (username,password,sex,phone,mail,head_img,address,introduce,regist_time) value('{$username}','{$password}','{$sex}','{$phone}','{$email}','{$head_img}','{$address}','{$introduce}','{$regist_time}')";
   		// var_dump($sql);
   		$link = M();
   		$res = $link->execute($sql);
   		$this->success('添加/成功',U('admin/user/successd'));


   	}else{
   		$this->display();
   	}
   

   }

    //****************编辑用户
   public function user_edit(){
   	if ($_GET) {
	   		$id =$_GET['id'];//获取要修改密码的用户的id
	   		// var_dump($id);
	   	}

   	if ($_POST) {
   		// var_dump($_POST);

   		//编辑用户
   		$username = I('post.user_name');
   		$password = md5(I('post.password'));
   		$sex = I('post.sex');
   		$phone = I('post.user_tel');
   		$email = I('post.email');
   		$head_img = "/Uploads/face/".I('post.uploadfile_2');
   		$address = I('post.demo1');
   		$introduce = I('post.introduce');
   		$regist_time = date('Y-m-d H:m:s');
   		$sql = "update user set username='{$username}',password='{$password}',sex='{$sex}',phone='{$phone}',mail='{$email}',address='{$address}',introduce='{$introduce}',regist_time='{$regist_time}' where id={$_GET['id']}";
   		// var_dump($sql);exit;
   		$link = M();
   		$res = $link->execute($sql);
   		$this->success('修改成功',U('admin/user/successd'));


   	}else{
   		$this->display();
   	}
   

   }



//**********************关闭框架
   public function successd()
   {
   		$this->display();
   }


//******************显示用户
   public function user_show()
   {
   	if ($_GET) {
   		// var_dump($_GET);
   		$link = M();
   		$sql = "select * from user where id={$_GET['id']}";
   		// var_dump($sql);
   		$data = current($link->query($sql));
   		// var_dump($data);
   		$this->assign('data',$data);
   	}
   		$this->display();
   	
   
   }


//*****************修改密码   
   public function password_edit(){
	   	if ($_GET) {
	   		$id =$_GET['id'];//获取要修改密码的用户的id
	   		
	   	}
	   	if ($_POST) {
	   		$password = md5(I('post.password'));
	   		$password2 = md5(I('post.password2'));
	   		if (empty($password) ){
	   			$this->error('密码不能为空,请再输入一次!');exit;
	   		}elseif ($password!=$password2) {
	   			$this->error('您两次输入的密码不一致!');exit;
	   		}else{
	   			$link = M();
	   			$sql = "update user set password='{$password2}' where id={$_GET['id']}";
	   			// var_dump($sql);
	   			$data = $link->execute($sql);
	   			$this->success('修改成功!',U('admin/user/successd'));
	   		}
	   	}else{

	   		$this->display();
	   	}
	}

//***************删除用户
	public function deleteuser()
	{
		if ($_GET) {
			// var_dump($_GET);
	   		$id =$_GET['id'];//获取要修改密码的用户的id
	   		$link = M();
	   		$sql = "update user set deleted ='1' where id={$_GET['id']}";
	   		$res = $link->execute($sql);
	   		$this->success('删除成功!',U('admin/user/index'));
	   	}

	}

	//***************恢复删除的用户
	public function recover()
	{
		if ($_GET) {
			// var_dump($_GET);
	   		$id =$_GET['id'];//获取要修改密码的用户的id
	   		$link = M();
	   		$sql = "update user set deleted ='0' where id={$_GET['id']}";
	   		$res = $link->execute($sql);
	   		$this->success('恢复成功!',U('admin/user/deleteuserlist'));
	   	}

	}

// ***************展示被删除的用户
	public function deleteuserlist()
	{
		//分页
    	$link = M();
   		$sql_all ="select count(*) cn from user where deleted ='1'";
   		$res =current($link->query($sql_all))['cn'];
   		 $Page = new \Think\Page($res,4);
   		 $show =$Page->show();

    	
    	//将用户数据输出到模板
   		$sql_user = "select * from user where deleted ='1' order by regist_time asc limit {$Page->firstRow},{$Page->listRows}";
   		$data = $link->query($sql_user);
   		// var_dump(count($data));


   		$this->assign('show',$show);//将分页输出
   		$this->assign('data',$data);//将用户数据输出到模板
        $this->display();
	}

//*********************工作管理
  public function work(){
      //分页
      $link = M();
      $sql_all ="select count(*) c from work ";
      $result =current($link->query($sql_all))['c'];
      // var_dump($result);
      $Page =new \Think\Page($result,5);
      $show =$Page->show();
      // var_dump($res_new);


      //输出工作内容
      $sql = "select w.*,u.username from work w left join user u on w.uid=u.id limit {$Page->firstRow},{$Page->listRows}";
      // var_dump($sql);
      $data = $link->query($sql);
      // var_dump($data);
      $this->assign('show',$show);//输出分页
      $this->assign('data',$data);//输出工作内容
      $this->display();
  }

//****************编辑工作
  public function work_edit(){
    if ($_GET) {
      $id =$_GET['id'];
      // var_dump($id);
      $link = M();
      $sql_user ="select u.username,w.* from work w left join user u on w.uid=u.id where w.id='{$id}'";
      $res =current($link->query($sql_user));
      // var_dump($res);
      $this->assign('res',$res);//输出用户名
    }
    if ($_POST) {
      $username =I('post.username');
      $company =I('post.company');
      $job =I('post.job_id');
      $start_y =I('post.start_y');
      $start_m =I('post.start_m');
      $start =$start_y."-".$start_m;
      $end_y =I('post.end_y');
      $end_m =I('post.end_m');
      $end =$end_y."-".$end_m;
      // var_dump($_POST);
      
      $sql ="update work set company='{$company}',job='{$job}',start='{$start}',end='{$end}' where id='{$id}'";
      // var_dump($sql);
      $result =$link->execute($sql);
      $this->success('修改成功!',U('admin/user/work'));
    }else{
      $this->display();
    }
    
  }






}